package com.xidian.zhiancloud.service.warnforecast.basicsset;

import com.xidian.zhiancloud.dao.KeyIndicatorWarnThresholdSetMapper;
import com.xidian.zhiancloud.dto.KeyIndicatorWarnSetDTO;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.KeyIndicatorWarnThresholdSet;
import com.xidian.zhiancloud.model.KeyIndicatorWarnThresholdSetExample;
import com.xidian.zhiancloud.model.WarnLevelSet;
import com.xidian.zhiancloud.model.WarnLevelSetExample;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 关键指标预警阈值设置
 */

@Service
public class KeyIndicatorWarnThresholdSetService {
    @Autowired
    private KeyIndicatorWarnThresholdSetMapper keyIndicatorWarnThresholdSetMapper;

    public List<KeyIndicatorWarnSetDTO> getList() {
        // 类型,0：风险分级管控，1：隐患排查治理，2：三违管理，3：系统运行情况
       int type[] = {0,1,2,3};
        List<int[]> ints = Arrays.asList(type);
        KeyIndicatorWarnSetDTO keyIndicatorWarnSetDTO = null;
        KeyIndicatorWarnThresholdSetExample exampleGenerator = new KeyIndicatorWarnThresholdSetExample();
        // 查询数据
        List<KeyIndicatorWarnThresholdSet> keyIndicatorWarnThresholdSets = keyIndicatorWarnThresholdSetMapper.selectByExample(exampleGenerator);

        List<KeyIndicatorWarnSetDTO> list  = new ArrayList<>();;
        int i;
        for (i = 0; i < 4; i++) {
            keyIndicatorWarnSetDTO = new KeyIndicatorWarnSetDTO();
            keyIndicatorWarnSetDTO.setType(i);
            List<KeyIndicatorWarnThresholdSet> keyIndicator = new ArrayList<>();
            for (KeyIndicatorWarnThresholdSet keyIndicatorWarnThresholdSet: keyIndicatorWarnThresholdSets){

                if ( i == keyIndicatorWarnThresholdSet.getType()){
                    keyIndicator.add(keyIndicatorWarnThresholdSet);
                }
                keyIndicatorWarnSetDTO.setKeyIndicatorWarnThresholdSetList(keyIndicator);
            }
            list.add(keyIndicatorWarnSetDTO);
        }
        return list;
    }

    public void update(KeyIndicatorWarnThresholdSet keyIndicatorWarnThresholdSet) {

        KeyIndicatorWarnThresholdSetExample example = new KeyIndicatorWarnThresholdSetExample();
        KeyIndicatorWarnThresholdSetExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(keyIndicatorWarnThresholdSet.getType());
        criteria.andWarnIndexTermEqualTo(keyIndicatorWarnThresholdSet.getWarnIndexTerm());

        KeyIndicatorWarnThresholdSet keyIndicator = new KeyIndicatorWarnThresholdSet();
        keyIndicator.setOneWarnValue(keyIndicatorWarnThresholdSet.getOneWarnValue());
        keyIndicator.setTwoWarnValue(keyIndicatorWarnThresholdSet.getTwoWarnValue());
        keyIndicator.setThreeWarnValue(keyIndicatorWarnThresholdSet.getThreeWarnValue());
        keyIndicator.setFourWarnValue(keyIndicatorWarnThresholdSet.getFourWarnValue());
        int count = keyIndicatorWarnThresholdSetMapper.updateByExampleSelective(keyIndicator, example);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void delete(KeyIndicatorWarnThresholdSet keyIndicatorWarnThresholdSet) {
        KeyIndicatorWarnThresholdSetExample example = new KeyIndicatorWarnThresholdSetExample();
        KeyIndicatorWarnThresholdSetExample.Criteria criteria = example.createCriteria();
         criteria.andIdEqualTo(keyIndicatorWarnThresholdSet.getId());
         criteria.andTypeEqualTo(keyIndicatorWarnThresholdSet.getType());
        int count = keyIndicatorWarnThresholdSetMapper.deleteByExample(example);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }

    public void insert(KeyIndicatorWarnThresholdSet keyIndicatorWarnThresholdSet) {
        int count = keyIndicatorWarnThresholdSetMapper.insert(keyIndicatorWarnThresholdSet);
        if (count == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
    }
}
